package sz.szsmk.citizencard

import android.app.Application
import android.content.Context
import android.util.Log
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import sz.szsmk.citizencard.ZHSZInit
import sz.szsmk.citizencard.model.network.bean.response.BaseBean
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback
import sz.szsmk.citizencard.retrofitrxjava.newversion.HttpDateSource
import sz.szsmk.citizencard.retrofitrxjava.newversion.SubscriberCallBack
import sz.szsmk.citizencard.ui.newversion.bean.AuthRequest
import sz.szsmk.citizencard.ui.newversion.bean.AuthRes
import sz.szsmk.citizencard.ui.newversion.config.Constants
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.JumpUtils

object ZHSZInit {

    //普通对象 不是application
    private var application: DaggerApplication? = null

    var appId = ""

    fun init(context: Application?) {

        if(appId.isEmpty()){
            Log.e("zhsz_Log","appid为空")
        }

        application = DaggerApplication()
        application!!.init(context)
        DaggerApplication.instance = application
    }


    fun setWxPayID(appId:String){
        this.appId = appId
    }


    fun jumpScan(context: Context, userid: String?, phoneNum: String?) {
        val authRequest = AuthRequest()
        authRequest.mobile = phoneNum
        authRequest.userid = userid

        HttpDateSource.createApi()?.authForSZ(authRequest)!!
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(SubscriberCallBack(object : ApiCallback<AuthRes?> {
                override fun onSuccess(response: AuthRes?) {
                    response?.let {

                        Log.e("jump", response.access_token)
                        ACache.get(context).put("access_token",response.access_token)
                        JumpUtils.jumptoNatvie(context, Constants.QRCODEACTIVITY, 0,null)
                    }
                }

                override fun onFailure(model: BaseBean?, e: Throwable?) {
                    model?.let {   }
                }
            }))

    }

    fun logout(context: Context){
        ACache.get(context).remove("access_token")
    }
}